package com.business.dashboard.controller;

import com.business.dashboard.entity.AdminUser;
import com.business.dashboard.service.AdminUserService;
import com.business.dashboard.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 管理员用户控制器
 *
 * @author Claude
 * @since 2025-07-06
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/admin-users")
@Validated
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    /**
     * 获取管理员用户列表
     */
    @GetMapping("/list")
    public ResultUtil<List<AdminUser>> getUserList() {
        try {
            List<AdminUser> users = adminUserService.getAllUsers();
            return ResultUtil.success("获取用户列表成功", users);
        } catch (Exception e) {
            log.error("获取用户列表失败", e);
            return ResultUtil.error("获取用户列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取管理员用户
     */
    @GetMapping("/{id}")
    public ResultUtil<AdminUser> getUserById(@PathVariable @NotNull Long id) {
        try {
            AdminUser user = adminUserService.getUserById(id);
            if (user != null) {
                return ResultUtil.success("获取用户信息成功", user);
            } else {
                return ResultUtil.error(404, "用户不存在");
            }
        } catch (Exception e) {
            log.error("获取用户信息失败，ID: {}", id, e);
            return ResultUtil.error("获取用户信息失败：" + e.getMessage());
        }
    }

    /**
     * 创建管理员用户
     */
    @PostMapping("/create")
    public ResultUtil<AdminUser> createUser(@RequestBody @Valid AdminUser user) {
        try {
            AdminUser createdUser = adminUserService.createUser(user);
            return ResultUtil.success("创建用户成功", createdUser);
        } catch (Exception e) {
            log.error("创建用户失败", e);
            return ResultUtil.error("创建用户失败：" + e.getMessage());
        }
    }

    /**
     * 更新管理员用户
     */
    @PutMapping("/update/{id}")
    public ResultUtil<AdminUser> updateUser(@PathVariable @NotNull Long id,
                                           @RequestBody @Valid AdminUser user) {
        try {
            user.setId(id);
            AdminUser updatedUser = adminUserService.updateUser(user);
            return ResultUtil.success("更新用户成功", updatedUser);
        } catch (Exception e) {
            log.error("更新用户失败，ID: {}", id, e);
            return ResultUtil.error("更新用户失败：" + e.getMessage());
        }
    }

    /**
     * 删除管理员用户
     */
    @DeleteMapping("/delete/{id}")
    public ResultUtil<String> deleteUser(@PathVariable @NotNull Long id) {
        try {
            boolean deleted = adminUserService.deleteUser(id);
            if (deleted) {
                return ResultUtil.success("删除用户成功");
            } else {
                return ResultUtil.error("删除用户失败");
            }
        } catch (Exception e) {
            log.error("删除用户失败，ID: {}", id, e);
            return ResultUtil.error("删除用户失败：" + e.getMessage());
        }
    }

    /**
     * 启用/禁用管理员用户
     */
    @PutMapping("/status/{id}")
    public ResultUtil<String> updateUserStatus(@PathVariable @NotNull Long id,
                                             @RequestParam @NotNull Integer status) {
        try {
            boolean updated = adminUserService.updateUserStatus(id, status);
            if (updated) {
                String message = status == 1 ? "启用用户成功" : "禁用用户成功";
                return ResultUtil.success(message);
            } else {
                return ResultUtil.error("更新用户状态失败");
            }
        } catch (Exception e) {
            log.error("更新用户状态失败，ID: {}, Status: {}", id, status, e);
            return ResultUtil.error("更新用户状态失败：" + e.getMessage());
        }
    }

}